package com.cskaoyan.service.impl;

import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.MarketAdminExample;
import com.cskaoyan.bean.lihaoming.DashBoardVo;
import com.cskaoyan.bean.zhangshizhi.web.admin.PageBO;
import com.cskaoyan.bean.zhangshizhi.web.admin.*;
import com.cskaoyan.mapper.AdminMapper;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.service.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author little ants
 * @since 2022/06/27 09:20
 */

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Autowired
    AdminMapper adminMapper;


    /**
     * 获取管理员列表的方法
     *
     * @param adminBO
     * @return com.cskaoyan.bean.zhangshizhi.web.admin.AdminListVO
     * @author little ants
     * @since 2022/06/27 19:12
     */
    @Override
    public AdminListVO getAdminList(PageBO adminBO) {
        // 利用marketAdminExample来设置查询条件
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (adminBO.getUsername() != null && !"".equals(adminBO.getUsername())) {
            criteria.andUsernameLike("%" + adminBO.getUsername()+ "%");
        }
        marketAdminExample.setOrderByClause(adminBO.getSort() + " " + adminBO.getOrder());

        PageHelper.startPage(adminBO.getPage(), adminBO.getLimit());

        List<MarketAdmin> list = marketAdminMapper.selectByExample(marketAdminExample);


        PageInfo<MarketAdmin> pageInfo = new PageInfo<>(list);

        AdminListVO adminListVO = new AdminListVO();

        adminListVO.setList(list);
        adminListVO.setLimit(adminBO.getLimit());
        adminListVO.setPage(adminBO.getPage());
        adminListVO.setTotal((int) pageInfo.getTotal());
        adminListVO.setPages(pageInfo.getPages());


        return adminListVO;
    }

    /**
     * 添加管理员的方法
     *
     * @param marketAdmin
     * @return void
     * @author little ants
     * @since 2022/06/27 19:13
     */
    @Override
    public void createAdmin(MarketAdmin marketAdmin) {
        Date date = new Date();
        marketAdmin.setAddTime(date);
        marketAdmin.setUpdateTime(date);
        marketAdminMapper.insertSelective(marketAdmin);

    }

    @Override
    public void removeAdmin(AdminDeleteBO adminDeleteBO) {
        marketAdminMapper.deleteAdmin(adminDeleteBO);
    }

    @Override
    public AdminUpdateVO updateAdmin(MarketAdmin marketAdmin) {
        marketAdminMapper.updateBymarketAdmin(marketAdmin);
        return null;
    }

    @Override
    public DashBoardVo queryTotal() {
        DashBoardVo dashBoardVo=adminMapper.select();
        return dashBoardVo;
    }





    // @Override
    // public void addAdmin(AdminCreateBO adminCreateBO) {
    //     Date date = new Date();
    //     marketAdminMapper.insertAdmin(adminCreateBO,date);
    //
    // }

    //
    // @Override
    // public void addAdmin2(MarketAdmin marketAdmin) {
    //     Date date = new Date();
    //     marketAdmin.setAddTime(date);
    //     marketAdmin.setUpdateTime(date);
    //     marketAdminMapper.insertSelective(marketAdmin);
    // }
}
